<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Orders extends Base{
    //全部订单
    public function orders(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input('param.openid');
        }else{
            return json(['code' => 503, 'msg' => 'openid呢']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        if(!empty(input("param.paystate"))){
            $paystate=input("param.paystate");
        }else{
            $paystate=0;
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                if($paystate==0){
                    $orders=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where(['a.paystate'=>[1,2,3,4,5,7]])->order('a.id desc')->limit(4*($page-1),4)->select();
                }else{
                    $orders=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.paystate',$paystate)->order('a.id desc')->limit(4*($page-1),4)->select();
                }
                if($orders){
                    for ($i=0; $i <count($orders) ; $i++) { 
                        $orders[$i]['imgarr']=explode(',',$orders[$i]['img']);
                        $orders[$i]['titlearr']=explode(',',$orders[$i]['title']);
                        $orders[$i]['pricearr']=explode(',',$orders[$i]['price']);
                        $orders[$i]['buynumarr']=explode(',',$orders[$i]['buynum']);
                        $orders[$i]['parmarr']=explode(';',$orders[$i]['parm']);
                        $orders[$i]['buynums']=$orders[$i]['buynum'];
                        if($orders[$i]['sub']==2){
                            $subs=Db::name('ordersub')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.of_id',$orders[$i]['id'])->select();
                            if(count($subs)>1){
                                array_pop($orders[$i]['imgarr']);
                                array_pop($orders[$i]['titlearr']);
                                array_pop($orders[$i]['pricearr']);
                                array_pop($orders[$i]['buynumarr']);
                                array_pop($orders[$i]['parmarr']);
                                $orders[$i]['buynums']=0;
                                for ($j=0; $j <count($subs) ; $j++) { 
                                    array_push($orders[$i]['imgarr'],$subs[$j]['img']);
                                    array_push($orders[$i]['titlearr'],$subs[$j]['title']);
                                    array_push($orders[$i]['pricearr'],$subs[$j]['price']);
                                    array_push($orders[$i]['buynumarr'],$subs[$j]['buynum']);
                                    array_push($orders[$i]['parmarr'],$subs[$j]['parm']);
                                    $orders[$i]['buynums']=$orders[$i]['buynums']+$subs[$j]['buynum'];
                                }
                            }
                        }
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$orders]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                if($paystate==0){
                    $orders=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where(['a.paystate'=>[1,2,3,4,5,7]])->order('a.id desc')->limit(4*($page-1),4)->select();
                }else{
                    $orders=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.paystate',$paystate)->order('a.id desc')->limit(4*($page-1),4)->select();
                }
                if($orders){
                    for ($i=0; $i <count($orders) ; $i++) { 
                        $orders[$i]['imgarr']=explode(',',$orders[$i]['img']);
                        $orders[$i]['titlearr']=explode(',',$orders[$i]['title']);
                        $orders[$i]['pricearr']=explode(',',$orders[$i]['price']);
                        $orders[$i]['buynumarr']=explode(',',$orders[$i]['buynum']);
                        $orders[$i]['parmarr']=explode(';',$orders[$i]['parm']);
                        $orders[$i]['buynums']=$orders[$i]['buynum'];
                        if($orders[$i]['sub']==2){
                            $subs=Db::name('ordersub')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.of_id',$orders[$i]['id'])->select();
                            if(count($subs)>1){
                                array_pop($orders[$i]['imgarr']);
                                array_pop($orders[$i]['titlearr']);
                                array_pop($orders[$i]['pricearr']);
                                array_pop($orders[$i]['buynumarr']);
                                array_pop($orders[$i]['parmarr']);
                                $orders[$i]['buynums']=0;
                                for ($j=0; $j <count($subs) ; $j++) { 
                                    array_push($orders[$i]['imgarr'],$subs[$j]['img']);
                                    array_push($orders[$i]['titlearr'],$subs[$j]['title']);
                                    array_push($orders[$i]['pricearr'],$subs[$j]['price']);
                                    array_push($orders[$i]['buynumarr'],$subs[$j]['buynum']);
                                    array_push($orders[$i]['parmarr'],$subs[$j]['parm']);
                                    $orders[$i]['buynums']=$orders[$i]['buynums']+$subs[$j]['buynum'];
                                }
                            }
                        }
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$orders]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                if($paystate==0){
                    $orders=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where(['a.paystate'=>[1,2,3,4,5,7]])->order('a.id desc')->limit(4*($page-1),4)->select();
                }else{
                    $orders=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->where('a.paystate',$paystate)->order('a.id desc')->limit(4*($page-1),4)->select();
                }
                if($orders){
                    for ($i=0; $i <count($orders) ; $i++) { 
                        $orders[$i]['imgarr']=explode(',',$orders[$i]['img']);
                        $orders[$i]['titlearr']=explode(',',$orders[$i]['title']);
                        $orders[$i]['pricearr']=explode(',',$orders[$i]['price']);
                        $orders[$i]['buynumarr']=explode(',',$orders[$i]['buynum']);
                        $orders[$i]['parmarr']=explode(';',$orders[$i]['parm']);
                        $orders[$i]['buynums']=$orders[$i]['buynum'];
                        if($orders[$i]['sub']==2){
                            $subs=Db::name('ordersub')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.of_id',$orders[$i]['id'])->select();
                            if(count($subs)>1){
                                array_pop($orders[$i]['imgarr']);
                                array_pop($orders[$i]['titlearr']);
                                array_pop($orders[$i]['pricearr']);
                                array_pop($orders[$i]['buynumarr']);
                                array_pop($orders[$i]['parmarr']);
                                $orders[$i]['buynums']=0;
                                for ($j=0; $j <count($subs) ; $j++) { 
                                    array_push($orders[$i]['imgarr'],$subs[$j]['img']);
                                    array_push($orders[$i]['titlearr'],$subs[$j]['title']);
                                    array_push($orders[$i]['pricearr'],$subs[$j]['price']);
                                    array_push($orders[$i]['buynumarr'],$subs[$j]['buynum']);
                                    array_push($orders[$i]['parmarr'],$subs[$j]['parm']);
                                    $orders[$i]['buynums']=$orders[$i]['buynums']+$subs[$j]['buynum'];
                                }
                            }
                        }
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$orders]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //取消付款
    public function ordercancel(){
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出订单id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 502, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('orders')->where('id',$order_id)->where('openid',$openid)->update(['paystate'=>5,'uptime'=>time()]);
        if($res){
            if(Db::name('orders')->where('id',$order_id)->value('sub')==2){
                Db::name('orderssub')->where('of_id',$order_id)->update(['paystate'=>5,'uptime'=>time()]);
            }
            return json(['code' => 200, 'msg' => '成功！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //删除订单
    public function orderdel(){
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出订单id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 502, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('orders')->where('id',$order_id)->where('openid',$openid)->update(['paystate'=>6,'uptime'=>time()]);
        if($res){
            if(Db::name('orders')->where('id',$order_id)->value('sub')==2){
                Db::name('orderssub')->where('of_id',$order_id)->update(['paystate'=>6,'uptime'=>time()]);
            }
            return json(['code' => 200, 'msg' => '成功！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //确认收货
    public function orderconfirm(){
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出订单id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 502, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('orders')->where('id',$order_id)->where('openid',$openid)->update(['paystate'=>4,'uptime'=>time()]);
        if($res){
            if(Db::name('orders')->where('id',$order_id)->value('sub')==2){
                Db::name('orderssub')->where('of_id',$order_id)->update(['paystate'=>4,'uptime'=>time()]);
            }
            return json(['code' => 200, 'msg' => '成功！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //催发货提醒
    public function expresstip(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.paycode"))){
            $paycode=input("param.paycode");
        }else{
            return json(['code' => 504, 'msg' => '请给出订单号']);exit();
        }
        if(!empty(input("param.title"))){
            $title=input("param.title");
        }else{
            return json(['code' => 505, 'msg' => '请给产品标题']);exit();
        }
        if(!empty(input("param.parm"))){
            $parm=input("param.parm");
        }else{
            return json(['code' => 506, 'msg' => '请给产品参数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if($customer['email_state']==1){
                $userinfo=Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where('state',1)->find();
                if($userinfo){
                    $subject='催发货提醒';
                    $allinfo='订单号：'.$paycode.'；<br>产品：'.$title.'；<br>参数：'.$parm.'；<br>联系人：'.$userinfo['uname'].'；<br>联系方式：'.$userinfo['uphone'];
                    $this->send_mail($customer['email_self'],$customer['email_code'],$subject,$allinfo);
                    return json(['code' => 200, 'msg' => '成功！']);exit();
                }else{
                    return json(['code' => 202, 'msg' => '默认地址未选中，无法通知对方自己的联系方式']);exit();
                }
            }elseif($customer['email_state']==2){
                return json(['code' => 201, 'msg' => '未开启提醒通知']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    private function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {
        $mail = new \PHPMailer\PHPMailer\PHPMailer();           //实例化PHPMailer对象
        $mail->CharSet = 'UTF-8';           //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP();                    // 设定使用SMTP服务
        $mail->SMTPDebug = 0;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';          // 使用安全协议
        $mail->Host = "smtp.qq.com"; // SMTP 服务器
        $mail->Port = 465;                  // SMTP服务器的端口号
        $mail->Username = $tomail;    // SMTP服务器用户名
        $mail->Password =$name;     // SMTP服务器密码，这里是你开启SMTP服务时生成密码
        $mail->SetFrom($tomail, '自己!');
        $replyEmail = '';                   //留空则为发件人EMAIL
        $replyName = '';                    //回复名称（留空则为发件人名称）
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($tomail,'自己');
        if (is_array($attachment)) { // 添加附件
            foreach ($attachment as $file) {
                is_file($file) && $mail->AddAttachment($file);
            }
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }
    //评论
    public function orderevaluate(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 504, 'msg' => '请给产品id']);exit();
        }
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 505, 'msg' => '请给订单id']);exit();
        }
        if(!empty(input("param.star"))){
            $star=input("param.star");
        }else{
            return json(['code' => 506, 'msg' => '请给星级']);exit();
        }
        if(!empty(input("param.message"))){
            $message=input("param.message");
        }else{
            return json(['code' => 507, 'msg' => '请给140字以内留言']);exit();
        }
        $img=input("param.img");
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $izevaluate=Db::name('evaluate')->where('cus_id',$cus_id)->where('p_id',$p_id)->where('openid',$openid)->where('order_id',$order_id)->find();
                if($izevaluate){
                    return json(['code' => 201, 'msg' => '评论过了']);exit();
                }else{
                    $data['cus_id']=$cus_id;
                    $data['openid']=$openid;
                    $data['p_id']=$p_id;
                    $data['order_id']=$order_id;
                    $data['star']=$star;
                    $data['message']=$message;
                    $data['img']=$img;
                    $data['time']=time();
                    $evaluate=Db::name('evaluate')->insert($data);
                    if($evaluate){
                        Db::name('orders')->where('id',$order_id)->update(['isevaluate'=>1]);
                        return json(['code' => 200, 'msg' => '成功']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败！']);exit();
                    }
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $izevaluate=Db::name('evaluate')->where('cus_id',$cus_id)->where('p_id',$p_id)->where('openid',$openid)->where('order_id',$order_id)->find();
                if($izevaluate){
                    return json(['code' => 201, 'msg' => '评论过了']);exit();
                }else{
                    $data['cus_id']=$cus_id;
                    $data['openid']=$openid;
                    $data['p_id']=$p_id;
                    $data['order_id']=$order_id;
                    $data['star']=$star;
                    $data['message']=$message;
                    $data['img']=$img;
                    $data['time']=time();
                    $evaluate=Db::name('evaluate')->insert($data);
                    if($evaluate){
                        Db::name('orders')->where('id',$order_id)->update(['isevaluate'=>1]);
                        return json(['code' => 200, 'msg' => '成功']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '失败！']);exit();
                    }
                }
            }else{
                return json(['code' => 508, 'msg' => '未开启此平台！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //我的评论
    public function myevaluate(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $myevaluates=Db::name('evaluate')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img as pimg,b.price')->where('a.cus_id',$cus_id)->where('a.openid',$openid)->order('a.id desc')->limit(4*($page-1),4)->select();
        if($myevaluates){
            for ($i=0; $i <count($myevaluates) ; $i++) { 
                $myevaluates[$i]['img']=explode(',', $myevaluates[$i]['img']);
                $myevaluates[$i]['time']=date('Y-m-d H:i:s',$myevaluates[$i]['time']);
            }
            return json(['code' => 200, 'msg' => '成功', 'data' => $myevaluates]);exit();
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);exit();
        }
    }
    public function suborders(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 504, 'msg' => '请给产品id']);exit();
        }
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 505, 'msg' => '请给订单id']);exit();
        }
        $suborders=Db::name('ordersub')->where('of_id',$order_id)->select();
        return json(['code' => 200, 'msg' => '成功', 'data' => $suborders]);exit();
    }
    public function orderdetail{
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 504, 'msg' => '请给订单id']);exit();
        }
        $order=Db::name('orders')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.id',$order_id)->find();
        $orders=array();
        if($order['sub']==1){
            $order['addtime']=date('Y-m-d H:i:s',$order['time']);
            $order['applytime']=date('Y-m-d H:i:s',$order['applytime']);
            array_push($orders, $order);
        }elseif($order['sub']==2){
            $orderarr=Db::name('ordersub')->alias('a')->join('shopgoods b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.of_id',$order_id)->select();
            for ($i=0; $i <count($orderarr) ; $i++) { 
                $orderarr[$i]['addtime']=date('Y-m-d H:i:s',$orderarr[$i]['time']);
                $orderarr[$i]['applytime']=date('Y-m-d H:i:s',$orderarr[$i]['applytime']);
                array_push($orders, $orderarr[$i]);
            }
        }
        $address=Db::name('address')->where('id',$order['address_id'])->find();
        $address['uphone']=substr_replace($address['uphone'], '****', 3, 4);
        return json(['code' => 200, 'msg' => '成功', 'address' => $address, 'orders' => $orders]);exit();
    }
}